/**
 * 
 */
package org.corpus.english.view;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.corpus.english.model.WordCategoryData;
import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;


/**
 * @author robbiecheng
 *
 */
public class WordCategorysConverter implements TypeConverter{

	public Object coerceToBean(Object val, Component comp) {
		Set items = (Set) val;
		Set cates = new HashSet(0);
		for (Iterator iterator = items.iterator(); iterator.hasNext();) {
			Listitem item = (Listitem) iterator.next();
			cates.add(item.getValue());
		}
		return cates;
	}

	public Object coerceToUi(Object val, Component comp) {
		Set cates = (Set) val;
		Listbox lbox = (Listbox) comp;
		Set items = new HashSet(0);
		for (Iterator iterator = cates.iterator(); iterator.hasNext();) {
			WordCategoryData cate = (WordCategoryData) iterator.next();
			items.add(lbox.getItemAtIndex(cate.getCategoryid() - 1));
		}
		return items;
	}

}
